package com.graduation.springbootinit.controller;

import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.model.entity.CulturePart.CulturePartComment;
import com.graduation.springbootinit.model.entity.User;
import com.graduation.springbootinit.service.CulturePartCommentService;
import com.graduation.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/culture/comment")
public class CulturePartCommentController {

    @Resource
    private CulturePartCommentService commentService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addComment(@RequestBody CulturePartComment comment, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        comment.setUserId(user.getId());
        comment.setUserName(user.getUserName());
        comment.setUserAvatar(user.getUserAvatar());
        commentService.addComment(comment);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteComment(@RequestParam Long id) {
        commentService.deleteComment(id);
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateComment(@RequestBody CulturePartComment comment) {
        commentService.updateComment(comment);
        return ResultUtils.success(true);
    }

    @GetMapping("/list")
    public BaseResponse<List<CulturePartComment>> getDiscussionComments(@RequestParam Long discussionId) {
        List<CulturePartComment> comments = commentService.getCommentsByDiscussionId(discussionId);
        return ResultUtils.success(comments);
    }

    @GetMapping("/replies")
    public BaseResponse<List<CulturePartComment>> getCommentReplies(@RequestParam Long commentId) {
        List<CulturePartComment> replies = commentService.getCommentReplies(commentId);
        return ResultUtils.success(replies);
    }

    @PostMapping("/like")
    public BaseResponse<Boolean> likeComment(@RequestParam Long commentId) {
        commentService.likeComment(commentId);
        return ResultUtils.success(true);
    }

    @PostMapping("/unlike")
    public BaseResponse<Boolean> unlikeComment(@RequestParam Long commentId) {
        commentService.unlikeComment(commentId);
        return ResultUtils.success(true);
    }

    @GetMapping("/info")
    public BaseResponse<String> getCommentInfo(@RequestParam Long commentId) {
        String commentInfo = commentService.getCommentInfo(commentId);
        return ResultUtils.success(commentInfo);
    }
}